home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / fbm12s.lha / fbsample.c < prev    next >
C/C++ Source or Header  |  1994-07-18  |  5KB  |  192 lines

  1. /*****************************************************************
  2.  * fbsample.c: FBM Release 1.2 07-Apr-93 Michael Mauldin
  3.  *
  4.  * Copyright (C) 1989-1993 by Michael Mauldin.  Permission is granted
  5.  * to use this file in whole or in part for any purpose, educational,
  6.  * recreational or commercial, provided that this copyright notice
  7.  * is retained unchanged.  This software is available to all free of
  8.  * charge by anonymous FTP and in the UUNET archives.
  9.  *
  10.  * fbsample.c:  1 bit to 8 bit conversion by sampling
  11.  *
  12.  * USAGE
  13.  *    % fbsample [ title ] < foo.pbm > foo.fbm
  14.  *
  15.  * EDITLOG
  16.  *    LastEditDate = Mon Jun 25 00:04:36 1990 - Michael Mauldin
  17.  *    LastFileName = /usr2/mlm/src/misc/fbm/fbsample.c
  18.  *
  19.  * HISTORY
  20.  * 07-Apr-93  Michael Mauldin (mlm) at Carnegie-Mellon University
  21.  *    Added -J switch
  22.  *
  23.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  24.  *    Package for Release 1.0
  25.  *
  26.  * 20-May-89  Michael Mauldin (mlm) at Carnegie Mellon University
  27.  *    Bug fix from Dave Cohrs <dave@cs.wisc.edu>
  28.  *
  29.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  30.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  31.  *
  32.  *  5-Sep-88  Michael Mauldin (mlm) at Carnegie-Mellon University
  33.  *    Created.
  34.  *****************************************************************/
  35.  
  36. #include <stdio.h>
  37. #include <ctype.h>
  38. #include "fbm.h"
  39.  
  40. int width, height;
  41.  
  42. # define USAGE \
  43.   "fbsample [ -t'title' -c'credits' -g'grain' -n'nbr' ]\n\
  44.      [ -<type> ] < bitmap > image"
  45.  
  46. #ifndef lint
  47. static char *fbmid =
  48. "$FBM fbsample.c <1.2> 07-Apr-93 (C) 1989-1993 by Michael Mauldin, source \
  49. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  50. #endif
  51.  
  52. main(argc, argv)
  53.   int             argc;
  54.   char           *argv[];
  55. {
  56.   char           *title = NULL, *credits = NULL;
  57.   register unsigned char *bmp, *obp;
  58.   register int      i, j, nbr=5, irow, k, r, c, orow;
  59.   int           nbrsqr, rndoff, grain=5, sum, average=0;
  60.   int          ipln, opln;
  61.   int          outtype = DEF_8BIT;
  62.   FBM          input, output;
  63.  
  64.   /* Clear the memory pointers so alloc_fbm won't be confused */
  65.   input.cm  = input.bm  = (unsigned char *) NULL;
  66.   output.cm = output.bm = (unsigned char *) NULL;
  67.  
  68.   /* Get the options */
  69.   while (--argc > 0 && (*++argv)[0] == '-')
  70.   { while (*++(*argv))
  71.     { switch (**argv)
  72.       { case 'g':    grain = atoi (*argv+1); SKIPARG; break;
  73.         case 'n':    nbr = atoi (*argv+1); SKIPARG; break;
  74.     case 't':    title = *argv+1; SKIPARG; break;
  75.     case 'c':    credits = *argv+1; SKIPARG; break;
  76.     case 'A':    outtype = FMT_ATK; break;
  77.     case 'B':    outtype = FMT_FACE; break;
  78.     case 'F':    outtype = FMT_FBM; break;
  79.     case 'G':    outtype = FMT_GIF; break;
  80.     case 'I':    outtype = FMT_IFF; break;
  81.     case 'J':    outtype = FMT_JPEG; break;
  82.     case 'L':    outtype = FMT_LEAF; break;
  83.     case 'M':    outtype = FMT_MCP; break;
  84.     case 'P':    outtype = FMT_PBM; break;
  85.     case 'R':    outtype = FMT_RLE; break;
  86.     case 'S':    outtype = FMT_SUN; break;
  87.     case 'T':    outtype = FMT_TIFF; break;
  88.     case 'X':    outtype = FMT_X11; break;
  89.     case 'Z':    outtype = FMT_PCX; break;
  90.     default:    fprintf (stderr, "Usage: %s\n", USAGE);
  91.             exit (1);
  92.       }
  93.     }
  94.   }
  95.  
  96.   if (grain < 1 || grain > 16)
  97.   { fprintf (stderr,
  98.          "Usage: %s\n%s\n",
  99.          "       (grain must be between 1 and 16)\n");
  100.     exit (0);
  101.   }
  102.  
  103.   /* Read pbm bitmap */
  104.   if (! read_bitmap (&input, (char *) NULL))
  105.   { fprintf (stderr, "%s: input garbled or not in PBM format\n", argv[0]);
  106.     exit(1);
  107.   }
  108.  
  109.   if (title == NULL && input.hdr.title[0])
  110.   { title = input.hdr.title; }
  111.  
  112.   if (grain > 1)
  113.   { width = input.hdr.cols / grain;
  114.     height = input.hdr.rows / grain;
  115.   }
  116.   else
  117.   { width = input.hdr.cols - nbr + 1;
  118.     height = input.hdr.rows - nbr + 1;
  119.   }
  120.   
  121.   width &= (~1);
  122.   height &= (~1);
  123.   
  124.   if (input.hdr.bits > 1 || input.hdr.planes > 1) average++;
  125.  
  126.   irow = input.hdr.rowlen;
  127.   ipln = input.hdr.plnlen;
  128.   orow = 2 * ((width+1)/2);
  129.   opln = orow * height;
  130.  
  131.   fprintf (stderr,
  132.        "Sample (%dbit to 8bit) \"%s\": [%dx%dx%d] ==> [%dx%dx8]\n",
  133.       input.hdr.bits,
  134.       title ? title : "(untitled)",
  135.       input.hdr.cols, input.hdr.rows, input.hdr.bits, width, height);
  136.  
  137.   /* Set up output header */
  138.   output.hdr.cols = width;
  139.   output.hdr.rows = height;
  140.   output.hdr.planes = input.hdr.planes;
  141.   output.hdr.bits = 8;
  142.   output.hdr.physbits = 8;
  143.   output.hdr.rowlen = orow;
  144.   output.hdr.plnlen = opln;
  145.   output.hdr.aspect = input.hdr.aspect;
  146.   output.hdr.clrlen = 0;
  147.  
  148.   if (title) strcpy (output.hdr.title, title);
  149.   if (credits) strcpy (output.hdr.credits, credits);
  150.   
  151.   alloc_fbm (&output);
  152.  
  153.   fprintf (stderr, "width %d, height %d, grain %d, nbr %d, irow %d, orow %d\n",
  154.        width, height, grain, nbr, irow, orow);
  155.  
  156.   nbrsqr = nbr * nbr;
  157.   rndoff = nbrsqr/2;
  158.   
  159.   for (k=0; k<input.hdr.planes; k++)
  160.   { for (r=0; r<height; r++)
  161.     { for (c=0; c<width; c++)
  162.       { sum = 0;
  163.         bmp = &(input.bm[k * ipln + (r*grain) * irow + (c*grain)]);
  164.     obp = &(output.bm[k * opln + r* orow + c]);
  165.  
  166.     if (average)
  167.     { for (j=0; j<nbr; j++, bmp += irow)
  168.           { for (i=0; i<nbr; i++)
  169.             { sum += bmp[i]; }
  170.           }
  171.  
  172.       sum = (sum + rndoff) / nbrsqr;
  173.     }
  174.     else
  175.     { for (j=0; j<nbr; j++, bmp += irow)
  176.           { for (i=0; i<nbr; i++)
  177.             { if (bmp[i]) sum++; }
  178.           }
  179.  
  180.           if (sum > 255) sum = 255;
  181.         }
  182.       
  183.         *obp++ = sum;
  184.       }
  185.     }
  186.   }
  187.  
  188.   write_bitmap (&output, stdout, outtype);
  189.  
  190.   exit (0);
  191. }
  192.